<?php
namespace app\cash\controller;
use think\Controller;
use think\Db;
use app\cash\model\OrderDetails;
use app\cash\model\StoreGoodsLog;
class Order extends Controller{
    /**
     * @Author lignyun
     * @Desc    检查登录
     * 2018-04-09
     */
    public function _initialize(){
        if(!session('cash_user')) {
            $this->redirect('@cash/login/index');
        }
    }

    /**
     * @Author  lingyun
     * @Desc    获取订单列表
     * 2018-06-11
     */
    public function index(){
        $order_type = input('order_type');
        $order_type = empty($order_type)?0:$order_type;
        $store_id = session('cash_user')['store_id'];
        $map['store_id'] = ['eq',$store_id];
        $map['pay_status'] = ['eq',1];
        $map['order_type'] = ['eq',0];

        if($order_type != 0){
            if($order_type == 2){           //预订单查询
                $map['is_yuding'] = ['eq',1];
                $map['ziqu_time'] = ['eq',0];
            }else if($order_type == 1){
                $t_time = $this->get_week_day();
                $map['addtime'] = array(array('gt',$t_time[0]),array('lt',$t_time[1]),'AND');
            }
        }else{
            $t_time = $this->get_week_day();
            $map['addtime'] = array(array('gt',$t_time[0]),array('lt',$t_time[1]),'AND');
        }

        $order_sn = input('key');
        if(!empty($order_sn)){
            $map['order_sn'] = ['eq',$order_sn];
        }

        $order = db('order')->where($map)->field('order_sn,id,final_amount,addtime,is_yuding,ziqu_time')->order('id desc')->limit(30)->select();

        $this->assign('key',$order_sn);
        $this->assign('order',$order);
        $this->assign('order_type',$order_type);

        return $this->fetch('index');
    }

    /**
     * @Author  lingyun
     * @Desc    获取订单信息
     */
    public function get_order_details(){
        $order_id = input('order_id');
        $order = db('order')->where('id',$order_id)->find();
        if(!empty($order['uid'])){
            $member = db('user')->where('uid',$order['uid'])->find();
        }else{
            $member = array();
        }

        $sql = "select case when LENGTH(b.name) < 20 then b.name else CONCAT(left(b.name,20),'...') end as name, case when LENGTH(a.spec_detail) < 20 then a.spec_detail else CONCAT(left(a.spec_detail,20),'...') end as spec_detail,a.spec_price,a.goods_num,a.all_discount_prcie,a.discount_num  from ys_order_details as a left join ys_goods as b on a.goods_id=b.id where a.order_id=".$order_id;

        $list = Db::query($sql);

        if(!empty($list)){
            $this->success('查询成功','',['list'=>$list,'member'=>$member,'final_amount'=>$order['final_amount'],'order_id'=>$order['id'],'pay_time'=>date('Y-m-d H:i',$order['pay_time']),'coupon_price'=>$order['discount_amount'],'pay_type'=>$order['pay_type'],'pay_type_two'=>$order['pay_type_two'],'goods_price'=>$order['goods_price'],'is_tuikuan'=>$order['is_tuikuan'],'ziqu_time'=>$order['ziqu_time']]);
        }else{
            $this->error('找不商品信息','');
        }
    }

    /**
     * @Author  lingyun
     * @Desc    获取最近一周的时间
     */
    private function get_week_day(){
        $timestamp = time();
        return [
            strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp))),
            strtotime(date('Y-m-d', strtotime("this week Sunday", $timestamp))) + 24 * 3600 - 1
        ];
    }

    /**
     * @Author  lingyun
     * @Desc    获取打印订单数据
     * 2018-06-11
     */
    public function get_print_data(){
        $order_id = input('order_id');

        $receipt = db('order')->where('id',$order_id)->value('receipt');

        if(empty($receipt)){
            $this->error('找不订单信息，请重试');
        }else{
            $this->success('成功','',$receipt);
        }
    }

    /**
     * @Author  lingyun
     * @Desc    取货
     * 2018-06-11
     */
    public function quhuo(){
        $order_id = input('order_id');
        $res = db('order')->where('id',$order_id)->update(['ziqu_time'=>time()]);
        if($res!==false){
            $this->success('取货成功','');
        }else{
            $this->error('操作失败，请重试','');
        }
    }

    /**
     * @Author  lingyun
     * @Desc    获取退款订单列表
     * 2018-06-12
     */
    public function get_tuikuan_order(){
        $order_sn = input('key');
        $order_type = input('order_type');
        $order_type = empty($order_type)?0:$order_type;
        $store_id = session('cash_user')['store_id'];

        $map['store_id'] = ['eq',$store_id];
        $map['pay_status'] = ['eq',1];
        $map['order_sn'] = ['eq',$order_sn];

        $order = db('order')->where($map)->field('order_sn,id,final_amount,addtime,is_yuding,ziqu_time,is_tuikuan')->select();

        $this->assign('key',$order_sn);
        $this->assign('order',$order);
        $this->assign('order_type',$order_type);

        return $this->fetch('index');

    }

    /**
     * @Author  lingyun
     * @Desc    删除订单
     * 2018-06-14
     */
    public function del_order(){
        $id = input('id');
        $order = db('order')->where('id',$id)->find();
        if($order['pay_status'] == 1){
            $this->error('该订单已支付，不能删除','');
        }
        $res = db('order')->where('id',$id)->delete();          //删除订单
        db('order_details')->where('order_id',$id)->delete();
        db('order_action')->where('order_id',$id)->delete();

        $res>0?$this->success('删除成功',''):$this->error('删除失败','');
    }


}